package com.keyancompany.zhongbaopingtai.utils;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by firefox on 2016/6/4.
 */
public class RegexUtils {


    /**
     * 手机
     */
    public static final int PHONE = 1;
    /**
     * 座机
     */
    public static final int MACHINE = 2;

    /**
     * 检测是否是合法的邮箱
     */
    public static final int EMAIL = 3;

    /**
     * 检测url是否为网址
     */
    public static final int INTERNATE = 4;

    /**
     * 全都是汉字
     */
    public static final int ALLHANZI = 5;

    /**
     * 检测密码
     */
    public static final int PASSWORD = 6;

    /**
     * 身份证idcard
     */
    public static final int IDCARD = 7;

    /**
     * 检测邮编
     */
    public static final int POSTCARD = 8;

    /**
     * 带有提示语的验证方法
     *
     * @param context     上下文
     * @param value       需要验证的内容
     * @param type        验证的类别 如：IDCARD、POSTCARD等
     * @param toastTextId
     * @return true:验证通过 false 验证未通过
     */
    public static boolean checkWithToast(Context context, String value,
                                         int type, int toastTextId) {
        boolean flag = false;
        if (TextUtils.isEmpty(value))
            return flag;
        switch (type) {
            case PHONE:
                flag = checkPhoneNumber(value);
                break;
            case MACHINE:
                flag = checkMachineNumber(value);
                break;
            case EMAIL:
                flag = checkEmail(value);
                break;
            case IDCARD:
                flag = checkNID(value);
                break;
            case ALLHANZI:
                flag = checkHanzi(value);
                break;
            case INTERNATE:
//                flag = checkInternetUrl(value);
                break;
            case POSTCARD:
                flag = checkPostCode(value);
                break;
            default:
                break;
        }
        if (!flag)
            Toast.makeText(context, toastTextId, Toast.LENGTH_SHORT).show();
        return flag;

    }

    /**********************************************
     * 检测手机号码是否符合格式
     *
     * @param number 手机号码
     * @return true 手机号码符合规范 flase 手机号不符合规范
     */
    public static boolean checkPhoneNumber(String number) {
        try {
            Pattern p = Pattern
                    .compile("(13[0-9]|14[57]|15[012356789]|18[02356789])\\d{8}");
            Matcher m = p.matcher(number);
            return m.matches();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**********************************************
     * 检查座机号码 可输入的格式: "XXX-XXXXXXX"、"XXXX-XXXXXXXX"、
     * "XXX-XXXXXXX"、"XXX-XXXXXXXX"、 "XXXXXXX"和"XXXXXXXX"
     *
     * @param machineNumber 座机号码
     * @return true 号码格式正确 false 格式有误
     */
    public static boolean checkMachineNumber(String machineNumber) {
        try {
            Pattern p = Pattern
                    .compile("([0-9]{7,8})|[0-9]{4}|[0-9]{3}-[0-9]{7,8}");
            Matcher m = p.matcher(machineNumber);
            return m.matches();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**********************************************
     * 检测Email是否符合规范
     *
     * @param email 检测Email
     * @return
     */
    public static boolean checkEmail(String email) {
        // 定义正则字符串
        String strPatten = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPatten);
        Matcher m = p.matcher(email);
        return m.matches();
    }


    /**********************************************
     * 检测字符串是否都是汉字
     *
     * @param hanzi
     * @return true 符合规范,全是汉字 false 不符合规范
     */
    public static boolean checkHanzi(String hanzi) {
        // 定义正则字符串
        String strPatten = "^[\u4e00-\u9fa5]{0,}$";
        Pattern p = Pattern.compile(strPatten);
        Matcher m = p.matcher(hanzi);
        return m.matches();
    }

    /**********************************************
     * 密码格式验证 必须以字母开头, 密码只能由数字,字母,下划线组成(6-16) /^[＼x80-＼xff_a-zA-Z][\w]{4,15}$/
     *
     * @param passWord
     * @return true 密码符合规范 false 密码不符合规范
     */
    public static boolean checkPassWord(String passWord, int minLength,
                                        int maxLength) {
        // 定义正则字符串
        String strPatten = "^[a-zA-Z]\\w{" + "".concat(minLength + "")
                .concat("," + maxLength).concat("}$");
        Pattern p = Pattern.compile(strPatten);
        Matcher m = p.matcher(passWord);
        return m.matches();
    }

    /**********************************************
     * 验证身份证号码
     *
     * @param number 身份证号码
     * @return true 身份证符合规范 false 身份证有误
     */
    public static Boolean checkNID(String number) {
        String pattern = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)\\d{4})((((19|20)(([02468][048])|([13579][26]))0229))|((20[0-9][0-9])|(19[0-9][0-9]))((((0[1-9])|(1[0-2]))((0[1-9])|(1\\d)|(2[0-8])))|((((0[1,3-9])|(1[0-2]))(29|30))|(((0[13578])|(1[02]))31))))((\\d{3}(x|X))|(\\d{4}))";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(number);
        return m.matches();
    }

    /**********************************************
     * 验证邮编是否合法
     *
     * @param postCode 邮编
     * @return true 邮编符合规范 false 邮编不符合规范
     */
    public static boolean checkPostCode(String postCode) {
        return postCode.matches("[1-9]\\d{5}(?!\\d)");

    }

    public static boolean validateNickName(Context context, EditText editText,
                                           int errorStringId) {
        String text = editText.getText().toString();

        if (TextUtils.isEmpty(text) || text.length() < 4 || text.length() > 20) {
            editText.setError(Html.fromHtml("<font color='red'>"
                    + context.getString(errorStringId) + "</font>"));
            return false;
        }
        return true;
    }

    /**
     * 验证长度
     *
     * @param context
     * @param editText
     * @param errorStringId
     * @return
     */
    public static boolean validateLength(Context context, int min, int max,
                                         EditText editText, int errorStringId) {
        String text = editText.getText().toString().trim();
        int lenght = text.length();

        // return matcher.matches();
        if (lenght < min || lenght > max) {
            editText.setError(Html.fromHtml("<font color='red'>"
                    + context.getString(errorStringId) + "</font>"));
            return false;
        }
        return true;
    }

    /**
     * ^\w{min,max}$
     *
     * @param context
     * @param editText            需要验证的EditText
     * @param errorStringId
     * @param minLength           最小位數
     * @param maxLength           最大位數
     * @param errorStringLengthId 提示长度语句
     * @return
     */
    public static boolean validatePassWord(Context context, EditText editText,
                                           int errorStringId, int minLength, int maxLength,
                                           int errorStringLengthId, int emptyStringId) {
        String text = editText.getText().toString().trim();
        if (TextUtils.isEmpty(text)) {
            editText.setError(Html.fromHtml("<font color='red'>"
                    + context.getString(emptyStringId) + "</font>"));
            return false;
        }
        if (text.length() > maxLength || text.length() < minLength) {
            editText.setError(Html.fromHtml("<font color='red'>"
                    + context.getString(errorStringLengthId) + "</font>"));
            return false;
        }
        String strPatten = "^\\w{" + minLength + "," + maxLength + "}$";
        Pattern p = Pattern.compile(strPatten);
        Matcher m = p.matcher(text);
        if (!m.matches()) {
            editText.setError(Html.fromHtml("<font color='red'>"
                    + context.getString(errorStringId) + "</font>"));
            return false;
        }
        return true;
    }

}
